package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * 员工管理
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param session
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpSession session, @RequestBody Employee employee){
        //1获取数据,密码加密
        String username = employee.getUsername();
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2判断用户名和密码是否存在
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,username);
        lqw.eq(Employee::getPassword,password);
        Employee emp = employeeService.getOne(lqw);
        if (emp==null){
            return R.error("用户名或密码有误");
        }
        //3判断状态是否禁用
        Integer status = emp.getStatus();
        if (status ==0){
           return R.error("账号已被禁用");
        }
        //4将用户存入session
        session.setAttribute("emp",emp.getId());
        //5返回登录结果
        return R.success(emp);
    }

    /**
     * 员工退出
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        //删除session数据
        session.removeAttribute("emp");
        return R.success("退出成功");
    }

    /**
     *  新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String>save(@RequestBody Employee employee){
        log.info("新增员工 {}",employee.toString());
        //密码加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("新增成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page>selectByPage(Integer page,Integer pageSize,String name){
        //构造分页构造器
        Page pageInfo=new Page(page,pageSize);
        //构建条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //name 不为null且部位空字符串才作为条件
        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //根据创建时间降序
        lqw.orderByDesc(Employee::getCreateTime);

        employeeService.page(pageInfo,lqw);

        return R.success(pageInfo);
    }

    /**
     * 修改员工数据
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee){

        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> select(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee!=null){
            return R.success(employee);
        }
        return R.error("没有此用户");
    }
}
